package com.patents.controller;

import com.alibaba.fastjson.JSON;
import com.patents.dao.PatentsMapper;

import com.patents.pojo.Patents;
import com.patents.pojo.QueryInfo;

import com.patents.service.PatentsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;

@Controller
@RestController
@CrossOrigin
public class PatentsController {
    @Autowired
    PatentsMapper patentsMapper;
    @Autowired
    /* UsersService usersService;*/
    @Qualifier("PatentsServiceImpl")
    public PatentsService patentsService;

    @RequestMapping("/addPatents")
    public String addPatents(@RequestBody Patents patents) {
        System.out.println("patent:*****" + patents);
        patentsService.addPatents(patents);
        return "ok";
    }
    @RequestMapping("/patentList")
    public String getPatentList(QueryInfo queryInfo) {
        System.out.println("访问成功" + queryInfo);
        HashMap<String, Object> data = new HashMap<>();
        List<Patents> PatentsList = patentsMapper.getPatentByUser(queryInfo.getQuery());
        System.out.println(PatentsList);
        data.put("groups", PatentsList);
        /*if(PatentsList!=null) {
            data.put("groups", PatentsList);
            data.put("flag", 200);}
        else {
            data.put("flag",404);}*/
        /*res.put("data",PatentsList);*/
        String g = JSON.toJSONString(data);
        return g;
    }

    @RequestMapping("/patentCheckList")
    public String getPatentCheckList(QueryInfo queryInfo) {
        System.out.println("访问成功" + queryInfo);
        HashMap<String, Object> data = new HashMap<>();
        List<Patents> PatentsList = patentsMapper.getPatentCheckByUser(queryInfo.getQuery());
        System.out.println(PatentsList);
        data.put("groups", PatentsList);
        String g = JSON.toJSONString(data);
        return g;
    }

    @RequestMapping("/deleteById")
    public String deleteById(QueryInfo queryInfo) {
        /*System.out.println("*******" + queryInfo);*/
        int patentId = Integer.parseInt(queryInfo.getQuery());//将String类型转为int类型
        /*System.out.println("****" + patentId);*/
        patentsService.deletePatentsById(patentId);
        return "ok";
    }

    @RequestMapping("/getUpdate")
    public String getUpdateUser(int id){
        Patents patents = patentsMapper.getUpdatePatent(id);
        String patents_json = JSON.toJSONString(patents);
        return patents_json;
    }

    /*修改专利*/
    @RequestMapping("/editPatent")
    public String editUser(@RequestBody Patents patents){
        /*System.out.println("@@@@@@@@@@"+patents);*/
        patentsMapper.editPatent(patents);
        /*int i = patentsMapper.editPatent(patents);
        String str = i >0?"success":"error";*/
        return "ok";
    }
    /*转让专利*/
    @RequestMapping("/zhuanrangPatent")
    public String zhuanRangUser(@RequestBody Patents patents){
        patentsMapper.zhuanRangPatent(patents);
        return "ok";
    }
    /*查询功能*/
    @RequestMapping("/searchByName")
    public String searchByName(QueryInfo queryInfo) {
        System.out.println("查询数据： "+queryInfo.getQuery());
        HashMap<String, Object> res = new HashMap<>();
        List<Patents> PatentsList = patentsMapper.getPatentByName("%"+queryInfo.getQuery()+"%");
        res.put("data", PatentsList);
        System.out.println("查询结果"+res);
        return JSON.toJSONString(res);
    }

}
